package com.smart.car.resource.init;

import com.smart.car.common.res.constant.Constants;
import com.smart.car.resource.entity.ResourceStallExample;
import com.smart.car.resource.mapper.ResourceStallMapper;
import com.smart.car.resource.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * TODO 有时需要项目服务启动时加载一些数据或预先完成某些动作的情景，就可以通过接口CommandLineRunner实现
 *  跟实现ApplicationRunner是一样的，这里为了演示两者作用效果，特意改为该方式。
 *
 * @author: zoutao
 * @date: 2021-05-06
 */
@Slf4j
@Order(1)
@Component
public class StartUPCommandLineRunner implements CommandLineRunner {

    @Autowired
    private RedisService redisService;

    @Autowired
    private ResourceStallMapper stallMapper;

    /**
     * 资源模块，缓存初始化（启动时就加载进去）
     */
    @Override
    public void run(String... args) throws Exception {
        if (!redisService.exist(Constants.cache.currentAviableStallAmt)) {
            ResourceStallExample example = new ResourceStallExample();
            long stalls = stallMapper.countByExample(example);
            redisService.cacheObject(Constants.cache.currentAviableStallAmt, stalls+"");
            log.info(Constants.cache.currentAviableStallAmt + " 该key值已缓存...");
        }
    }
}
